package com.example.todolist.util

import android.Manifest
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_HIGH
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.os.Build
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.example.todolist.R

object NotificationUtil {
    fun createNotification(
        context: Context,
        notiTtle: String,
        content: String,
        channelId: String,
        channelName: String
    ): Notification {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel(channelId, channelName, context)
//            manager.notify(1, notification)
        } else {
            ""
        }
        return NotificationCompat.Builder(context, channelId)
            .setContentTitle(notiTtle)
            .setContentText(content)
            .setSmallIcon(R.raw.icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.raw.icon))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true) // 点击后自动清除通知
            .build()
    }


    @RequiresApi(Build.VERSION_CODES.O)
    fun createNotificationChannel(
        channelId: String,
        channelName: String,
        context: Context
    ): String {
        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//        if (!manager.areNotificationsEnabled()) {
//            // 未开启通知权限，弹出提示框引导用户开启
//        showNotificationPermissionDialog(context)
//        }
        val channel = NotificationChannel(channelId, channelName, IMPORTANCE_HIGH)
        manager.createNotificationChannel(channel)
        return channelId
    }

    private fun showNotificationPermissionDialog(context: Context) {
        android.app.AlertDialog.Builder(context)
            .setTitle("通知权限申请")
            .setMessage("为了您能及时收到应用通知，请开启通知权限。")
            .setPositiveButton("去开启") { _, _ ->
                val intent = Intent()
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                }
                context.startActivity(intent)
            }
            .setNegativeButton("取消", null)
            .show()
    }

    fun showPermissionDialog(context: Context) {
        if (ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.POST_NOTIFICATIONS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                context as Activity,
                arrayOf(Manifest.permission.POST_NOTIFICATIONS),
                1
            )
        } else {

        }
    }
}